package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.Project;
import com.ruoyi.market.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 服务项目信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/project")
public class ProjectController extends BaseController
{
    @Autowired
    private IProjectService projectService;

    /**
     * 获取服务项目列表
     */
    @PostMapping("/projectList")
    public TableDataInfo list(@RequestBody Project project, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            project.setShopId(shopId);
        }
        startPageByPost();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    @Log(title = "服务项目管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('market:project:export')")
    @PostMapping("/projectExport")
    public void export(HttpServletResponse response, Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        util.exportExcel(response, list, "服务项目数据");
    }

    @Log(title = "服务项目管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('market:project:import')")
    @PostMapping("/projectImportData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        List<Project> projectList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = projectService.importProject(projectList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/projectImportTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        util.importTemplateExcel(response, "服务项目数据");
    }

    /**
     * 根据服务项目编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:project:query')")
    @GetMapping(value = { "/", "/{projectId}" })
    public AjaxResult getInfo(@PathVariable(value = "projectId", required = false) Long projectId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(projectId))
        {
            Project sysProject = projectService.selectProjectById(projectId);
            ajax.put(AjaxResult.DATA_TAG, sysProject);
        }
        return ajax;
    }

    /**
     * 新增服务项目
     */
    @PreAuthorize("@ss.hasPermi('market:project:add')")
    @Log(title = "服务项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Project project, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            project.setShopId(shopId);
        }
        if (!projectService.checkProjectTitleUnique(project))
        {
            return error("新增'" + project.getProjectTitle() + "'失败，已存在相同数据");
        }
        project.setCreateBy(getUsername());
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改服务项目
     */
    @PreAuthorize("@ss.hasPermi('market:project:edit')")
    @Log(title = "服务项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Project project)
    {
        if (!projectService.checkProjectTitleUnique(project))
        {
            return error("修改'" + project.getProjectTitle() + "'失败，已存在相同数据");
        }
        project.setUpdateBy(getUsername());
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除服务项目
     */
    @PreAuthorize("@ss.hasPermi('market:project:remove')")
    @Log(title = "服务项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds)
    {
        return toAjax(projectService.deleteProjectByIds(projectIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('market:project:edit')")
    @Log(title = "服务项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/projectChangeStatus")
    public AjaxResult changeStatus(@RequestBody Project project)
    {
        project.setUpdateBy(getUsername());
        return toAjax(projectService.updateProjectStatus(project));
    }

    /**
     * 随机单是否可用
     */
    @PreAuthorize("@ss.hasPermi('market:project:edit')")
    @Log(title = "服务项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/projectChangeIsAble")
    public AjaxResult changeIsAble(@RequestBody Project project)
    {
        project.setUpdateBy(getUsername());
        return toAjax(projectService.updateProjectIsAble(project));
    }

    /**
     * 获取服务项目树列表
     */
    @PreAuthorize("@ss.hasPermi('market:project:list')")
    @GetMapping("/projectTree")
    public AjaxResult deptTree(Project project, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            project.setShopId(shopId);
        }
        return success(projectService.selectProjectTreeList(project));
    }
}
